package com.example.introductiontoprogramming;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Programming5 extends AppCompatActivity {
    double y;

    private EditText edtTxtMaina;
    private Button btnMainJisuan;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.programming_main5);
        initView();
    }

    private void initView() {
        edtTxtMaina = (EditText) findViewById(R.id.edtTxtMaina);
        btnMainJisuan = (Button) findViewById(R.id.btnMainJisuan);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }

    public void resultonClick(View view) {
        String a = edtTxtMaina.getText().toString();
        if (a.length() == 0) {
            Toast.makeText(Programming5.this, "请输入x的值", Toast.LENGTH_SHORT).show();
        } else {
            double x = Double.parseDouble(a);
            if (x < 1) {
                y = x;
                tvMainResult.setText(String.valueOf(y));
            } else if (1 <= x && x < 10) {
                y = 2 * x - 1;
                String str = String.format("%.2f", y);
                tvMainResult.setText(String.valueOf(str));
            } else if (x >= 10) {
                y = 3 * x - 11;
                String str = String.format("%.2f", y);
                tvMainResult.setText(String.valueOf(str));
            }
        }
    }
}